/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.seckill.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.seckill.common.model.SeckillOrder;
import com.yami.shop.seckill.common.service.SeckillOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author LGH
 * @date 2019-08-28 09:36:59
 */
@RestController
@RequestMapping("/seckill/seckillOrder")
@Api(tags = "商家端秒杀订单接口")
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查找秒杀订单信息")
    public ServerResponseEntity<IPage<SeckillOrder>> getSeckillOrderPage(PageParam<SeckillOrder> page, SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.page(page, new LambdaQueryWrapper<SeckillOrder>()));
    }

    @GetMapping("/info/{seckillOrderId}")
    @ApiOperation(value = "通过id查询秒杀订单")
    @ApiImplicitParam(name = "seckillOrderId", value = "秒杀订单id", required = true, dataType = "Long")
    public ServerResponseEntity<SeckillOrder> getById(@PathVariable("seckillOrderId") Long seckillOrderId) {
        return ServerResponseEntity.success(seckillOrderService.getById(seckillOrderId));
    }

    @PostMapping
    @ApiOperation(value = "新增秒杀订单")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.save(seckillOrder));
    }

    @PutMapping
    @ApiOperation(value = "修改秒杀订单")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.updateById(seckillOrder));
    }

    @DeleteMapping("/{seckillOrderId}")
    @ApiOperation(value = "通过id删除秒杀订单")
    @ApiImplicitParam(name = "seckillOrderId", value = "秒杀订单id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long seckillOrderId) {
        return ServerResponseEntity.success(seckillOrderService.removeById(seckillOrderId));
    }

    @GetMapping("/getSeckillIdByOrderNumber")
    @ApiOperation(value = "根据订单编号获取秒杀id")
    @ApiImplicitParam(name = "orderNumber", value = "订单编号", required = true, dataType = "String")
    public ServerResponseEntity<Long> getSeckillIdByOrderNumber(@RequestParam("orderNumber") String orderNumber) {
        SeckillOrder seckillOrder = seckillOrderService.getOne(new LambdaQueryWrapper<SeckillOrder>().eq(SeckillOrder::getOrderNumber, orderNumber));
        if (Objects.isNull(seckillOrder)) {
            throw new YamiShopBindException("yami.data.deleted.or.not.exist");
        }
        return ServerResponseEntity.success(seckillOrder.getSeckillId());
    }
}
